//
//  UIImageView+YYangWebImageManager.m
//  YYangWebImageManagerProject
//
//  Created by 蓓蕾 on 2021/11/29.
//

#import "UIImageView+YYangWebImageManager.h"

@implementation UIImageView (YYangWebImageManager)

- (void)yyang_setImageWithURLString:(NSString *)urlString
{
    [self yyang_setImageWithURLString:urlString placeholderImage:nil options:0 progress:nil completed:nil];
}

- (void)yyang_setImageWithURLString:(NSString *)urlString placeholderImage:(nullable UIImage *)placeholder
{
    [self yyang_setImageWithURLString:urlString placeholderImage:placeholder options:0 progress:nil completed:nil];
}

-(void)yyang_setImageWithURLString:(NSString *)urlString completed:(nullable YYImageViewCompletionBlock)completedBlock
{
    [self yyang_setImageWithURLString:urlString placeholderImage:nil options:0 progress:nil completed:completedBlock];
}

-(void)yyang_setImageWithURLString:(NSString *)urlString placeholderImage:(nullable UIImage *)placeholder completed:(nullable YYImageViewCompletionBlock)completedBlock
{
    [self yyang_setImageWithURLString:urlString placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
}

- (void)yyang_setImageWithURLString:(NSString *)urlString placeholderImage:(nullable UIImage *)placeholder options:(YYImageViewOptions)options completed:(nullable YYImageViewCompletionBlock)completedBlock
{
    [self yyang_setImageWithURLString:urlString placeholderImage:placeholder options:options progress:nil completed:completedBlock];
}

- (void)yyang_setImageWithURLString:(NSString *)urlString placeholderImage:(nullable UIImage *)placeholder options:(YYImageViewOptions)options progress:(nullable YYImageViewDownloaderProgressBlock)progressBlock completed:(nullable YYImageViewCompletionBlock)completedBlock
{
    NSURL *url = [NSURL URLWithString:urlString];
    if (![urlString hasPrefix:@"http"]) {
        urlString = [NSString stringWithFormat:@"%@%@",YYangImageHost,urlString];
        url = [NSURL URLWithString:urlString];
    }
    SDWebImageOptions sdWebImageOptions = (SDWebImageOptions)options;
    
    
    [self sd_setImageWithURL:url placeholderImage:placeholder options:sdWebImageOptions progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        if (progressBlock) {
            progressBlock(receivedSize, expectedSize);
        }
    } completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (completedBlock) {
            YYImageViewCacheType yycacheType = (YYImageViewCacheType)cacheType;
            completedBlock(image, error, yycacheType, imageURL);
        }
    }];
}



- (void)yyang_setImageWithURL:(NSURL *)url
{
    [self yyang_setImageWithURLString:url.absoluteString placeholderImage:nil options:0 progress:nil completed:nil];
}

- (void)yyang_setImageWithURL:(NSURL *)url placeholderImage:(nullable UIImage *)placeholder
{
    [self yyang_setImageWithURLString:url.absoluteString placeholderImage:placeholder options:0 progress:nil completed:nil];
}

-(void)yyang_setImageWithURL:(NSURL *)url completed:(nullable YYImageViewCompletionBlock)completedBlock
{
    [self yyang_setImageWithURLString:url.absoluteString placeholderImage:nil options:0 progress:nil completed:completedBlock];
}

-(void)yyang_setImageWithURL:(NSURL *)url placeholderImage:(nullable UIImage *)placeholder completed:(nullable YYImageViewCompletionBlock)completedBlock
{
    [self yyang_setImageWithURLString:url.absoluteString placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
}

- (void)yyang_setImageWithURL:(NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(YYImageViewOptions)options completed:(nullable YYImageViewCompletionBlock)completedBlock
{
    [self yyang_setImageWithURLString:url.absoluteString placeholderImage:placeholder options:options progress:nil completed:completedBlock];
}

- (void)yyang_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(YYImageViewOptions)options progress:(nullable YYImageViewDownloaderProgressBlock)progressBlock completed:(nullable YYImageViewCompletionBlock)completedBlock
{
    [self yyang_setImageWithURLString:url.absoluteString placeholderImage:placeholder options:options progress:progressBlock completed:completedBlock];
}

@end
